/*
 * DAOFactory.java
 *
 * Created on 19 settembre 2007, 14.51
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package it.cnr.icib.daopattern;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Nino
 */
//import model.DAO.AnomalieDAO;
/**
 *
 * @author Nino
 */
public abstract class DAOFactory <T extends DAOOperationsInterface> {
    // Lista dei titpi di databses supportati dalla fattoria
    // List of DAO types supported by the factory
    @Deprecated
    public static final int ACCESS = 1;
    @Deprecated
    public static final int MYSQL = 2;
    @Deprecated
    public static final int POSTGRSQL = 3;
    @Deprecated
    public static final int XML = 4;
    @Deprecated
    public static final int XSB = 5;
    @Deprecated
    public static final int ORACLE = 6;
    @Deprecated
    public static final int SQLLite = 7;

    /**
     * @param aStm the stm to set
     */
    public static void setStm(Statement aStm) {
        stm = aStm;
    }

    // Definizione dei metodi per ogni DAO che può essere creato
    // Le fattotir concrete implemeneterrano questi metodi
    // There will be a method for each DAO that can be created. 
    // The concrete factories will have to implement these methods.
   
    private static Statement stm = null;
    public static Connection conn;

    
    public abstract T getBeansDAO();
    
    
    /**
     * 
     * @param classDAO:  parametro di tipo Class. La classe data in input deve estendere la classe DAOFactory
     * @return DAOFactory: Ritorna l'upcasting di un'istanza della classe fornita in input 
     * @throws InstantiationException
     * @throws IllegalAccessException 
     */
    public static DAOFactory getDAOFactory(Class classDAO) throws InstantiationException, IllegalAccessException {

        if ((classDAO.getSuperclass().getName()).compareTo("it.cnr.icib.daopattern.DAOFactory") == 0) {
            return  (DAOFactory) classDAO.newInstance();    
        } else {
            System.out.println("La classe " + classDAO + " deve estendere la classe it.cnr.icib.daopattern.DAOFactory .");
            return null;
        }


    }
    
    
    

    @Deprecated
    public static DAOFactory getDAOFactory(int whichFactory, String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException {

        Class c = Class.forName(className);
      
        
        if( (c.getSuperclass().getName()).compareTo("daopattern.DAOFactory")==0){
           
           System.out.println("Estende la classe giusta ;) ");
       }else{
           System.out.println("NON Estende la classe giusta ;) -->" + c.getSuperclass().getName());
           System.out.println("La classe "+ className + " deve estendere la classe daopattern.DAOFactory.");
       }
       
        return (DAOFactory) c.newInstance();    
        
        
        /*   switch (whichFactory) {
            // case ACCESS: 
            //    return new AccessDAOFactory();
            // case MYSQL    : 
            //    return new MySqlDAOFactory();      
            //case POSTGRSQL    : 
            //return new PostgreSqlDAOFactory();
            //return new PostgreSqlDAO_Car_Washing();
            // case XML    : 
            //   return new XMLlDAOFactory();
            // case XSB    : 
            //   return new XSBDAOFactory();
            // case ORACLE    : 
            //  return new OracleDAOFactory();
            case SQLLite:
                //return new SqlLiteDAO_TaskModel();
                return (DAOFactory) c.newInstance();
            default:
                return null;
        }
        */
    }

    
    
    
    
    
    /**
     * Creates a new instance of DAOFactory
     */
    public DAOFactory() {}

    public static Statement getNew_Statement() {
        try {
            return conn.createStatement();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static Statement getStatement() {
        if (stm != null) {
            return stm;
        } else {
            try {
                stm=conn.createStatement();
                return stm;
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            return null;
        }

    }

    public static Connection getConn() {
        return conn;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }
    
    
    public  static String getCanonicalNameOfClass(String className) throws ClassNotFoundException{
        return (Class.forName(className)).getCanonicalName(); 
    }
    
}
